package com.tzxs.pay.main.onlineaut;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivitySendMsgBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.ViewDialog;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 发送信息
 */
public class SendMsgActivity extends BaseActivity<ActivitySendMsgBinding> {

    private BillViewModel model;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_msg;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding.sendMsgToolbar.setNavigationOnClickListener(v -> onBackPressed());
        model = ViewModelProviders.of(this).get(BillViewModel.class);

        //入住人
        String inPeople = getIntent().getStringExtra("inPeople");
        //手机号
        String phone = getIntent().getStringExtra("phone");
        //入住时间
        String time = getIntent().getStringExtra("time");
        //备注
        String note = getIntent().getStringExtra("note");
        //金额
        String amount = getIntent().getStringExtra("amount");

        //短信内容
        String msgContent = getString(R.string.msg_content, inPeople, amount);
        dataBinding.sendContent.setText(msgContent);
        dataBinding.receivePhone.setText(phone);

        model.snackbarText.observe(this, s -> {
            if (s != null) {

                Log.d("SendMsgActivity", "onCreate: 提示消息:===" + s);
                Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            }
        });

        //发送短信
        dataBinding.send.setOnClickListener(v -> editMsg(inPeople, phone, time, note, amount));


    }

    //开始编辑短信
    private void editMsg(String inPeople, String phone, String time,
                         String note, String amount) {
        //短信接收手机号
        String receivePhone = dataBinding.receivePhone.getText().toString();
        if (TextUtils.isEmpty(receivePhone)) {
            Toast.makeText(this, "请输入短信接收手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        model.createMsgAut(Float.parseFloat(amount), inPeople, phone, receivePhone, time, note, () -> {
            if (model.msgAutOrderId.get() != null) {

                String content = "在线预授权订单生成成功  ¥" + amount + " " + getCurrentTime();
                model.startGt(ISharedPreference.getInstance(getApplication()).getCid(), "天择可付推送提醒", content, () -> {

                });
                showDialog();
            }


        });

    }

    //显示打印dialog
    private void showDialog() {
        ViewDialog viewDialog = new ViewDialog();
        viewDialog.setLayoutId(R.layout.deposit_dialog);
        viewDialog.showViewListener(view -> {
            TextView title = view.findViewById(R.id.accept_dialog_title);
            title.setText("短信已发送");
            TextView ok = view.findViewById(R.id.ok);
            ok.setOnClickListener(v -> {
                viewDialog.dismiss();
                Intent intent = new Intent(SendMsgActivity.this, OnlineAdActivity.class);
                intent.putExtra("orderId", model.msgAutOrderId.get());
                startActivity(intent);
                finish();
            });
        });
        viewDialog.show(getSupportFragmentManager(), "0");
    }

    //获取当前时间
    private String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        simpleDateFormat.applyPattern("MM-dd HH:mm:SS");

        return simpleDateFormat.format(new Date());
    }

}
